{% extends 'stocks/base.html' %}

{% block title %}{{ title }}{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col-12">
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{% url 'stock_list' %}">股票列表</a></li>
                    <li class="breadcrumb-item"><a href="{% url 'stock_detail' stock.symbol %}">{{ stock.symbol }} - {{ stock.name }}</a></li>
                    <li class="breadcrumb-item active">买入信号</li>
                </ol>
            </nav>
            <h2>
                <i class="bi bi-bell"></i> {{ stock.symbol }} - {{ stock.name }} 买入信号
            </h2>
        </div>
    </div>

    <!-- 统计信息卡片 -->
    <div class="row mb-4">
        <div class="col-md-4">
            <div class="card text-center">
                <div class="card-body">
                    <h5 class="card-title text-primary">{{ total_signals }}</h5>
                    <p class="card-text">总信号数</p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card text-center">
                <div class="card-body">
                    <h5 class="card-title text-info">{{ avg_strength|floatformat:1 }}</h5>
                    <p class="card-text">平均强度</p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card text-center">
                <div class="card-body">
                    <h5 class="card-title text-success">{{ days }}</h5>
                    <p class="card-text">统计天数</p>
                </div>
            </div>
        </div>
    </div>

    <!-- 筛选表单 -->
    <div class="card mb-4">
        <div class="card-header">
            <h5 class="card-title mb-0">筛选条件</h5>
        </div>
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-3">
                    <label for="days" class="form-label">统计天数</label>
                    <select class="form-select" id="days" name="days">
                        <option value="7" {% if days == 7 %}selected{% endif %}>最近7天</option>
                        <option value="30" {% if days == 30 %}selected{% endif %}>最近30天</option>
                        <option value="90" {% if days == 90 %}selected{% endif %}>最近90天</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label for="min_strength" class="form-label">最小强度</label>
                    <select class="form-select" id="min_strength" name="min_strength">
                        <option value="0" {% if min_strength == 0 %}selected{% endif %}>所有强度</option>
                        <option value="50" {% if min_strength == 50 %}selected{% endif %}>≥50</option>
                        <option value="60" {% if min_strength == 60 %}selected{% endif %}>≥60</option>
                        <option value="70" {% if min_strength == 70 %}selected{% endif %}>≥70</option>
                        <option value="80" {% if min_strength == 80 %}selected{% endif %}>≥80</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label for="strategy" class="form-label">策略筛选</label>
                    <select class="form-select" id="strategy" name="strategy">
                        <option value="">所有策略</option>
                        {% for strategy in strategies %}
                            <option value="{{ strategy.id }}" {% if strategy_id == strategy.id|stringformat:"s" %}selected{% endif %}>
                                {{ strategy.name }}
                            </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">&nbsp;</label>
                    <div>
                        <button type="submit" class="btn btn-primary">应用筛选</button>
                        <a href="{% url 'stock_signals' stock.symbol %}" class="btn btn-outline-secondary">重置</a>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <!-- 信号列表 -->
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5 class="card-title mb-0">买入信号列表</h5>
            <span class="badge bg-secondary">{{ page_obj.paginator.count }} 条记录</span>
        </div>
        <div class="card-body">
            {% if page_obj %}
                <div class="table-responsive">
                    <table class="table table-striped table-hover">
                        <thead>
                            <tr>
                                <th>信号日期</th>
                                <th>策略名称</th>
                                <th>信号价格</th>
                                <th>信号强度</th>
                                <th>买入理由</th>
                                <th>生成时间</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for signal in page_obj %}
                                <tr>
                                    <td>{{ signal.signal_date|date:"Y-m-d" }}</td>
                                    <td>
                                        <a href="{% url 'strategy_detail' signal.strategy_id %}" class="text-decoration-none">
                                            {{ signal.strategy_name }}
                                        </a>
                                    </td>
                                    <td>¥{{ signal.signal_price|floatformat:2 }}</td>
                                    <td>
                                        <span class="badge {% if signal.signal_strength >= 80 %}bg-danger{% elif signal.signal_strength >= 60 %}bg-warning{% else %}bg-info{% endif %}">
                                            {{ signal.signal_strength|floatformat:1 }}
                                        </span>
                                    </td>
                                    <td>{{ signal.reason }}</td>
                                    <td>{{ signal.created_at|date:"Y-m-d H:i" }}</td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>

                <!-- 分页 -->
                {% if page_obj.has_other_pages %}
                    <nav aria-label="Page navigation">
                        <ul class="pagination justify-content-center">
                            {% if page_obj.has_previous %}
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if days %}&days={{ days }}{% endif %}{% if min_strength %}&min_strength={{ min_strength }}{% endif %}{% if strategy_id %}&strategy={{ strategy_id }}{% endif %}">上一页</a>
                                </li>
                            {% endif %}

                            {% for num in page_obj.paginator.page_range %}
                                {% if page_obj.number == num %}
                                    <li class="page-item active"><span class="page-link">{{ num }}</span></li>
                                {% else %}
                                    <li class="page-item">
                                        <a class="page-link" href="?page={{ num }}{% if days %}&days={{ days }}{% endif %}{% if min_strength %}&min_strength={{ min_strength }}{% endif %}{% if strategy_id %}&strategy={{ strategy_id }}{% endif %}">{{ num }}</a>
                                    </li>
                                {% endif %}
                            {% endfor %}

                            {% if page_obj.has_next %}
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if days %}&days={{ days }}{% endif %}{% if min_strength %}&min_strength={{ min_strength }}{% endif %}{% if strategy_id %}&strategy={{ strategy_id }}{% endif %}">下一页</a>
                                </li>
                            {% endif %}
                        </ul>
                    </nav>
                {% endif %}
            {% else %}
                <div class="alert alert-info">
                    <i class="bi bi-info-circle"></i> 未找到符合条件的买入信号。
                </div>
            {% endif %}
        </div>
    </div>

    <!-- 操作按钮 -->
    <div class="row mt-4">
        <div class="col-12">
            <div class="d-flex gap-2">
                <a href="{% url 'stock_list' %}" class="btn btn-secondary">
                    <i class="bi bi-arrow-left"></i> 返回股票列表
                </a>
                <a href="{% url 'stock_detail' stock.symbol %}" class="btn btn-outline-primary">
                    <i class="bi bi-info-circle"></i> 查看股票详情
                </a>
                <a href="{% url 'analyze_stock' stock.symbol %}" class="btn btn-primary">
                    <i class="bi bi-lightning"></i> 立即分析
                </a>
            </div>
        </div>
    </div>
</div>
{% endblock %}
